package com.gzbugu.bugusys.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gzbugu.bugusys.dao.IBaseDAO;

public class BaseDAO extends HibernateDaoSupport implements IBaseDAO {
	private static final Log log = LogFactory.getLog(BaseDAO.class);

	@Override
	public void attachClean(Object instance) {
		log.debug("attaching clean " + instance.getClass().getName()
				+ " instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public void attachDirty(Object instance) {
		log.debug("attaching dirty " + instance.getClass().getName()
				+ " instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	@Override
	public void delete(Object persistentInstance) {
		log.debug("deleting " + persistentInstance.getClass().getName()
				+ " instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object findById(Serializable id, Class cls) {
		log.debug("getting " + cls.getName() + " instance with id: " + id);
		try {
			Object instance = getHibernateTemplate().get(cls, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List findByProperty(String propertyName, Object value, Class cls) {
		log.debug("finding " + cls.getName() + " instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from " + cls.getName()
					+ " as model where model." + propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public Object merge(Object detachedInstance) {
		log.debug("merging " + detachedInstance.getClass().getName()
				+ " instance");
		try {
			Object result = getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	@Override
	public void save(Object transientInstance) {
		log.debug("saving " + transientInstance.getClass().getName()
				+ " instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List findAll(Class cls) {
		log.debug("finding all " + cls.getName() + " instances");
		try {
			String queryString = "from " + cls.getName();
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countRow(Class cls) {
		String queryString = "select count(*) from " + cls.getName();
		List result = getHibernateTemplate().find(queryString);
		if (!result.isEmpty()) {
			return ((Long) result.get(0)).intValue();
		}
		return 0;
	}

}
